import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import InfoShow from '../views/InfoShow'
import FoundList from '../views/FoundList'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    component: () => import(/* webpackChunkName: "index" */ '../views/Index.vue'),
    children: [
      {
        path: '/',
        component: Home
      }, {
        path: '/home',
        name: 'home',
        component: Home
      }, {
        path: '/infoShow',
        name: 'infoShow',
        component: InfoShow
      }, {
        path: '/foundList',
        name: 'foundList',
        component: FoundList
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '*',
    name: '404',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "404" */ '../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let IsLoain = !localStorage.eleToken
  if (to.path == '/login' || to.path == '/register') {
    next()
  } else {
    !IsLoain ? next() : next('/login')
  }
})

export default router
